fn main() {
    let r1: Rectangle = Rectangle {
        width: 20,
        height: 30,
    };

    println!("rec1: {:?}", r1); // debug
    println!("rec1: {:#?}", r1); // format debug

    let r2 = Rectangle {
        width: 10,
        ..r1 //解构
    };
    println!("rec2: {:#?}", r2);

    let width: u32 = 3;
    let height: u32 = 14;
    let r3 = Rectangle { width, height };
    println!("rec3: {:#?}", r3);
    println!("area: {}", r3.area());

    if r3.width() {
        println!("r3 has right width");
    }

    println!("r1 can hold r2?: {}.", r1.can_hold(&r2));
    println!("r2 can hold r3?: {}.", r2.can_hold(&r3));

    println!("square: {:#?}", Rectangle::square(20));

}

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    // method
    fn area(&self) -> u32 {
        self.width * self.height
    }

    // method: same as attribute
    fn width(&self) -> bool {
        if self.width > 0 {
            true
        } else {
            false
        }
    }
}

// multi impl block
impl Rectangle {
    // more param
    fn can_hold(&self, other: &Self) -> bool {
        self.width > other.width && self.height > other.height
    }

    // 关联函数： 通常用来返回结构体对象实例或做与结构体无关的事务
    fn square(size: u32) -> Self {
        Rectangle{
            width: size,
            height: size
        }
    }

    // fn square(size: u32) -> Rectangle {
    //     Rectangle{
    //         width: size,
    //         height: size
    //     }
    // }
}
